﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TAP
{
    class Tapper
    {
        /*
         http://www.codeproject.com/Articles/646239/NET-Asynchronous-Patterns
         http://www.codeproject.com/Tips/591586/Asynchronous-Programming-in-Csharp-5-0-using-async         
         */

        static void Main(string[] args)
        {
            Console.WriteLine("Statring in Main....");
            Start().Wait();            
        }


        static async Task Start()
        {   
            int result = await GetPrimeCountAsync(5,10000000);
            Console.WriteLine(result);
            
            await Console.In.ReadLineAsync();
        }


        static async Task<int> GetPrimeCountAsync(int min, int count)
        {
            Console.WriteLine("Staring to fetch primes for the range (min,count): " + min + ", " + count);

            return await Task.Run<int>(() =>
            {
                var res = ParallelEnumerable.Range(min, count)
                                         .Count(n => Enumerable.Range(2, (int) Math.Sqrt(n) - 1)
                                         .All(i => n % i > 0));
                Console.WriteLine("Done fetching...");

                return res;
                
            });

            
        } 

    }


}
